package com.example.dao.impl;

import com.example.dao.InvoiceRecordDao;
import com.example.entity.customerAdmin.InvoiceRecord;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class InvoiceRecordDaoImpl implements InvoiceRecordDao {
    @Override
    public int add (InvoiceRecord invoiceRecord) {
         int i=-1;
        try {
            Object[] objects=new Object[6];
            objects[0]=invoiceRecord.getDate ();
            objects[1]=invoiceRecord.getMoney ();
            objects[2]=invoiceRecord.getType ();
            objects[3]=invoiceRecord.getInvoiceUser ();
            objects[4]=invoiceRecord.getEnterTime ();
            objects[5]=invoiceRecord.getRelationOrder ();

            String sql="insert into invoicerecord(date,money,type,invoiceUser,enterTime,relationOrder) value(?,?,?,?,?,?)";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int del (int id) {
        int i=-1;
        try {
            Object[] objects=new Object[1];
            objects[0]=id;
            i= BaseDao.modify ("delete from invoicerecord where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int update (InvoiceRecord invoiceRecord) {
        int i=-1;
        try {
            Object[] objects=new Object[7];
            objects[0]=invoiceRecord.getDate ();
            objects[1]=invoiceRecord.getMoney ();
            objects[2]=invoiceRecord.getType ();
            objects[3]=invoiceRecord.getInvoiceUser ();
            objects[4]=invoiceRecord.getEnterTime ();
            objects[5]=invoiceRecord.getRelationOrder ();
            objects[6]=invoiceRecord.getId ();
            String sql="update invoicerecord set date=?,money=?,type=?,invoiceUser=?,enterTime=?,relationOrder=? where id=?";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<InvoiceRecord> selectAll (int page,int limit) {

        List<InvoiceRecord> invoiceRecords=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from invoicerecord limit ?,?", objects);
        try {
            while (select.next ()){
                InvoiceRecord invoiceRecord=new InvoiceRecord ();
                invoiceRecord.setId (select.getInt ("id"));
                invoiceRecord.setDate (select.getString ("date"));
                invoiceRecord.setMoney (select.getInt ("money"));
                invoiceRecord.setType (select.getString ("type"));
                invoiceRecord.setInvoiceUser (select.getString ("invoiceUser"));
                invoiceRecord.setEnterTime (select.getString ("enterTime"));
                invoiceRecord.setRelationOrder (Integer.parseInt(select.getString ("relationOrder")));
                invoiceRecords.add (invoiceRecord);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return invoiceRecords;
    }

    @Override
    public int getCount () {
        int i=-1;
        ResultSet select = BaseDao.select ("select count(1) getCount from invoicerecord ", null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public InvoiceRecord selectId (int id) {
        InvoiceRecord invoiceRecord=new InvoiceRecord ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from invoicerecord where id=?", objects);
        try {
           while (select.next ()){
               invoiceRecord.setId (select.getInt ("id"));
               invoiceRecord.setDate (select.getString ("date"));
               invoiceRecord.setMoney (select.getInt ("money"));
               invoiceRecord.setType (select.getString ("type"));
               invoiceRecord.setInvoiceUser (select.getString ("invoiceUser"));
               invoiceRecord.setEnterTime (select.getString ("enterTime"));
               invoiceRecord.setRelationOrder (Integer.parseInt(select.getString ("relationOrder")));
                return invoiceRecord;
           }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return null;
    }
}
